package com.troubadorian.feeds;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.troubadorian.feeds.R;
import com.fivehundredpixels.fivehundredpixels.model.Photo;
import com.troubadorian.feeds.adapters.GalleryImageAdapter;
import com.troubadorian.feeds.utils.ImageCacheLoader;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ImageView.ScaleType;

public class PhotoDetailActivity extends Activity
{
	private Photo myPhoto = null;

	private int mResponse;

	private ProgressBar myProgress;

	private static final String TAG = "PhotoGridActivity";

	private ImageCacheLoader imageCacheLoader;

	private ImageView selectedImageView;

	private ImageView leftArrowImageView;

	private ImageView rightArrowImageView;

	private Gallery gallery;
	
	private int selectedImagePosition = 0;
	
	private List<Drawable> mDrawablesSmall = new ArrayList<Drawable>();
	
	private List<Drawable> mDrawablesLarge = new ArrayList<Drawable>();
	
	private GalleryImageAdapter galImageAdapter;
	
	private int mNumPhotos = 20;

	private int mPhotoId;

	private class DataLoadingTask extends AsyncTask<Void, Void, Void>
	{

		@Override
		protected void onPostExecute(Void result)
		{
			display();
		}

		@Override
		protected void onPreExecute()
		{

		}

		@Override
		protected Void doInBackground(Void... params)
		{

			Log.d(TAG, "----" + FeaturesConfiguration.fetchPhotoURLBase
					+ mPhotoId + "?comments&consumer_key="
					+ FeaturesConfiguration.consumer_key);

			try
			{

				myPhoto = DataManager.getInstance().fetchPhotoByGet(
						PhotoDetailActivity.this,
						FeaturesConfiguration.fetchPhotoURLBase + mPhotoId
								+ "?comments&consumer_key="
								+ FeaturesConfiguration.consumer_key, true);

			} catch (Exception ex)
			{
				Log.e(TAG, ex.toString());
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Void... values)
		{
			super.onProgressUpdate(values);
		}

	}

	private void display()
	{
		if (myPhoto != null)
		{
			Log.d(TAG,
					"---------------the display method and myList is populated"
							+ myPhoto.id);

			imageCacheLoader.DisplayImage(myPhoto.images.get(0).url,
					selectedImageView);
			
			
			

		} else
		{
			Log.d(TAG, "-----------the list returned was null");
		}

		if (myProgress != null)
		{
			myProgress.setVisibility(View.GONE);
			myProgress = null;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo_detail);
		
		getDrawablesList();

		myProgress = (ProgressBar) this.findViewById(R.id.progressbar);

		selectedImageView = (ImageView) this
				.findViewById(R.id.selectedimageview);

		leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);

		leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
		
		rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);
		
		gallery = (Gallery) findViewById(R.id.gallery);
		
		leftArrowImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v)
			{

				if (selectedImagePosition > 0)
				{
					--selectedImagePosition;

				}

				gallery.setSelection(selectedImagePosition, false);
			}
		});

		rightArrowImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v)
			{

				if (selectedImagePosition < mDrawablesSmall.size() - 1)
				{
					++selectedImagePosition;

				}

				gallery.setSelection(selectedImagePosition, false);

			}
		});
		
		gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
			{

				selectedImagePosition = pos;

				if (selectedImagePosition > 0 && selectedImagePosition < mDrawablesSmall.size() - 1)
				{

					leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
					rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));

				} else if (selectedImagePosition == 0)
				{

					leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));

				} else if (selectedImagePosition == mDrawablesSmall.size() - 1)
				{

					rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
				}

				changeBorderForSelectedImage(selectedImagePosition);
				setSelectedImage(selectedImagePosition);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{

			}

		});
		
		galImageAdapter = new GalleryImageAdapter(this, mDrawablesSmall);

		gallery.setAdapter(galImageAdapter);

		if (mDrawablesSmall.size() > 0)
		{

			gallery.setSelection(selectedImagePosition, false);

		}

		if (mDrawablesSmall.size() == 1)
		{

			rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
		}


		mPhotoId = this.getIntent().getIntExtra("photoid", 39260702);

		imageCacheLoader = new ImageCacheLoader(this);

		try
		{

			myProgress.setVisibility(View.VISIBLE);
		} catch (Exception ex)
		{
			Log.e(TAG, "----exception" + ex.toString());
		}

		new DataLoadingTask().execute();

	}
	private void changeBorderForSelectedImage(int selectedItemPos)
	{

		int count = gallery.getChildCount();

		for (int i = 0; i < count; i++)
		{

			ImageView imageView = (ImageView) gallery.getChildAt(i);
			imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_border));
			imageView.setPadding(3, 3, 3, 3);

		}

		ImageView imageView = (ImageView) gallery.getSelectedView();
		imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.selected_image_border));
		imageView.setPadding(3, 3, 3, 3);
	}
	
	private void getDrawablesList()
	{
		Log.d(TAG, "---------------getDrawablesList");
		
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic1));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic2));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic3));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic4));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic5));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic6));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic7));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic8));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic9));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic1));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic2));
		mDrawablesSmall.add(getResources().getDrawable(R.drawable.pic3));
		
		/* 
		for (int i=0; i < mNumPhotos; i++)
		{
			File imgFile = new  File(PhotoDetailActivity.this.getApplicationContext().getCacheDir() + "/" + mTradeId + "_" + i);
			Log.d(TAG, "---------------getDrawablesList" + imgFile.toString());
			if(imgFile.exists()){

			    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

			    mDrawables.add(new BitmapDrawable(getResources(),myBitmap));
			    
			    Log.d(TAG, "-----exists--------" + PhotoDetailActivity.this.getApplicationContext().getCacheDir() + "/" + mTradeId + "_" + i);
			    
			}
		}
		*/

	}

	private void setSelectedImage(int selectedImagePosition)
	{

		BitmapDrawable bd = (BitmapDrawable) mDrawablesSmall.get(selectedImagePosition);
		Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7),
				false);
		selectedImageView.setImageBitmap(b);
		selectedImageView.setScaleType(ScaleType.FIT_XY);

	}

}
